Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 22-Sep-2010 (Jagdish)

Public Class frmMessages
    Dim mstrMode As String
    Public blnAdd As Boolean, blnEdit As Boolean, blnDelete As Boolean, blnLocked As Boolean
    Dim mlngRecordID As Long
    Dim blnUnload As Boolean
    Dim mblnChanged As Boolean
    Dim mblnActivate As Boolean
    Dim mblnExpanded As Boolean = False
    Dim mblnAutoSize As Boolean = True
    Dim gridView1 As Views.Grid.GridView
    Dim mblnCancelDisplay As Boolean
    Public mblnCheck As Boolean = False
    Public mintSenderID As Integer

    Private Sub frmMessages_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Try
            If mblnActivate = True Then
                mblnActivate = False

                Call PopulateCheckedListboxDX(chklistUsers, "FullName", "UserId", "vwUserDetails", "UserId,FullName", "UserId<>" & gintUserID, "FullName")
                Call PopulateLookUpName()
                If mblnCheck = True Then
                    With chklistUsers
                        For intCtr As Integer = 0 To .ItemCount
                            If mintSenderID = .GetItemValue(intCtr) Then
                                .SetItemCheckState(intCtr, CheckState.Checked)
                            End If
                        Next intCtr
                    End With
                End If

            End If

        Catch ex As Exception
            pbRecords.Visible = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmMessages_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            mblnCheck = False
            'Dim intUnload As DialogResult

            'If mblnChanged = True And mstrMode <> "" Then
            '    intUnload = MessageBox.Show("You have not saved your changes to the record. Do you want to save your changes now?", gstrMessage, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            '    If intUnload = Windows.Forms.DialogResult.Cancel Then
            '        e.Cancel = True : Exit Sub
            '    ElseIf intUnload = Windows.Forms.DialogResult.Yes Then
            '        blnUnload = True
            '        If blnSaveRecords() = False Then
            '            e.Cancel = True
            '            blnUnload = False
            '            Exit Sub
            '        End If
            '        mblnChanged = False
            '    End If
            'End If
            'blnUnload = False
            gblnIsRefreshed = True
        Catch ex As Exception
            blnUnload = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub PopulateLookUpName()
        Dim dsTemp As DataSet = FillDataSet("select PatientId,PatientTitle + ' ' + (IsNull(LastName + ', ' , '') + FirstName + Isnull(' ' + MiddleName, '') ) [Patient Name]" & _
                                            ",Address1 [Address],DateOfBirth [Birth Date] from tblPatientInfo where void=0 order by LastName")
        With lkPatient.Properties
            .DataSource = dsTemp.Tables(0)
            .DisplayMember = "Patient Name"
            .ValueMember = "PatientId"
            .NullText = ""
            .ImmediatePopup = True
            .ShowFooter = False
            .View.OptionsSelection.EnableAppearanceFocusedCell = False
            .View.OptionsView.AutoCalcPreviewLineCount = True
            .View.OptionsFilter.AllowColumnMRUFilterList = True
            .View.OptionsFilter.AllowFilterEditor = True
            .View.OptionsFilter.AllowMRUFilterList = True
            .View.OptionsView.ShowAutoFilterRow = True
            .View.OptionsView.ShowIndicator = False
            .View.OptionsView.ShowGroupPanel = False
            .View.OptionsView.ShowPreview = True
            .View.OptionsFilter.ShowAllTableValuesInFilterPopup = True
            .ImmediatePopup = True
            .ShowFooter = False
            .View.Columns("PatientId").Visible = False
            lkPatient.EditValue = -1
        End With
    End Sub

    Private Sub frmMessages_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            Call SetImageControl(Me)
            Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgFormIcons.Images(2), Bitmap).GetHicon())
            Call GetAccessPermission(Me, "frmMessages")
            mblnActivate = True
            mblnChanged = False
            btnSend.Enabled = False
            

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        Dim NoUserChecked As Boolean = False
        Dim intCtr As Integer = 0
        IsValidEntry = False

        With chklistUsers
            For intCtr = 0 To .ItemCount
                If .GetItemChecked(intCtr) Then
                    NoUserChecked = True
                    Exit For
                End If
            Next intCtr
        End With
        If NoUserChecked = False Then
            MessageBox.Show("Please select receipient(s) to send message.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            chklistUsers.Focus()
        End If

        If IsDBNullOrNothing(meMessage.Text) Then
            strMsg = "Please enter message to send."
            meMessage.Focus()
        End If

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Function blnSaveRecords() As Boolean
        Dim blnIsTransactionOpen As Boolean
        Dim strQuery As String
        Try
            blnSaveRecords = False

            If IsValidEntry() = False Then
                Exit Function
            End If


            gtrnWebPlusMed = gconWebPlusMed.BeginTransaction
            blnIsTransactionOpen = True
            Dim intCtr As Integer = 0

            With chklistUsers
                For intCtr = 0 To .ItemCount - 1
                    If .GetItemChecked(intCtr) Then


                        strQuery = gintUserID
                        strQuery = strQuery & "," & .GetItemValue(intCtr)
                        strQuery = strQuery & "," & SmartReplace(meMessage.Text)
                        strQuery = strQuery & ",0"
                        strQuery = strQuery & ",'" & Now & "'"
                        If IsDBNullOrNothing(lkPatient.EditValue) Then
                            strQuery = strQuery & ",Null"
                        Else
                            If Val(lkPatient.EditValue) <= 0 Then
                                strQuery = strQuery & ",Null"
                            Else
                                strQuery = strQuery & "," & Val(lkPatient.EditValue)
                            End If

                        End If
                        Call InsertintoTable("tblMessages", strQuery)
                    End If
                Next intCtr
            End With

           
            blnSaveRecords = True

            gtrnWebPlusMed.Commit()
            blnIsTransactionOpen = False
            MessageBox.Show("Message sent successfully.", gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Information)
            gblnIsRefreshed = True
        Catch ex As Exception
            If blnIsTransactionOpen = True Then
                gtrnWebPlusMed.Rollback()
            End If
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Function

    Private Sub ClearControls()
        Try
            Call Clearfields(grbControls)
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnViewMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewMessage.Click

    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Try
            If blnSaveRecords() = False Then
                Exit Sub
            End If
            meMessage.Text = ""
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub chklistUsers_ItemCheck(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ItemCheckEventArgs) Handles chklistUsers.ItemCheck
        Dim NoUserChecked As Boolean = False
        Dim intCtr As Integer = 0
        With chklistUsers
            For intCtr = 0 To .ItemCount
                If .GetItemChecked(intCtr) Then
                    NoUserChecked = True
                    Exit For
                End If
            Next intCtr
        End With
        If NoUserChecked = False Then
            btnSend.Enabled = False
        Else
            btnSend.Enabled = True
        End If
    End Sub

    Private Sub meMessage_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles meMessage.KeyDown
        NotesFocus(meMessage)
    End Sub

    Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectAll.CheckedChanged
        With chklistUsers
            For intCount As Integer = 0 To .ItemCount - 1
                .SetItemCheckState(intCount, chkSelectAll.CheckState)
            Next
        End With
    End Sub

    Private Sub lkPatient_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkPatient.ButtonClick
        If e.Button.Index = 1 Then
            Call PopulateLookUpName()
        End If
    End Sub

    Private Sub lkPatient_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lkPatient.EditValueChanged
        mblnChanged = True
    End Sub
End Class
